<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html>
<head>
<title>Events in Java Gnome</title>
<link rel="stylesheet" href="/cfg/format.css" type="text/css">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="keywords" content="GUI, Java Gnome, events, Java, programming, Linux">
<meta name="description" content="Events in Java Gnome">
<meta name="language" content="en">
<meta name="author" content="Jan Bodnar">
<meta name="distribution" content="global">

<script type="text/javascript" src="/lib/jquery.js"></script>
<script type="text/javascript" src="/lib/common.js"></script>

</head>

<body>

<div class="container">

<div id="wide_ad" class="ltow">
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* 160x600, August 2011 */
google_ad_slot = "2484182563";
google_ad_width = 160;
google_ad_height = 600;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>

<div class="content">


<a href="/" title="Home">Home</a>&nbsp;
<a href="..">Contents</a>


<h1>Events in Java Gnome</h1>

<p>
In this part of the Java Gnome programming tutorial, we will talk about events.
</p>

<div class="center">
<script type="text/javascript"><!--
google_ad_client = "ca-pub-9706709751191532";
/* top_horizontal */
google_ad_slot = "3327173442";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>

<p>
Java Gnome library is an event driven system. All GUI applications are event driven. 
The applications start a main loop, which continuously checks for newly generated
 events. If there is no event, the application waits and does nothing. 
</p>

<h2>Simple event example</h2>

<p>
The next example shows, how we react to two basic events. 
</p>

<div class="codehead">quitbutton.java</div> 
<pre class="code">
package com.zetcode;

import org.gnome.gdk.Event;
import org.gnome.gtk.Button;
import org.gnome.gtk.Fixed;
import org.gnome.gtk.Gtk;
import org.gnome.gtk.Widget;
import org.gnome.gtk.Window;
import org.gnome.gtk.WindowPosition;

/**
 * Java Gnome tutorial
 *
 * This program demonstrates two 
 * basic events. 
 *
 * @author jan bodnar
 * website zetcode.com
 * last modified March 2009
 */

public class GButton extends Window  {

    public GButton() {
    
        setTitle("Button");
    
        initUI();

        setPosition(WindowPosition.CENTER);
        showAll();
    }
    
    public void initUI() {
    
        connect(new Window.DeleteEvent() {
            public boolean onDeleteEvent(Widget source, Event event) {
                Gtk.mainQuit();
                return false;
            }
        });
    
        Fixed fixed = new Fixed();
        
        Button quit = new Button("Quit");
        quit.connect(new Button.Clicked() {

            public void onClicked(Button button) {
                Gtk.mainQuit();
            }
        });

        quit.setSizeRequest(80, 35);
        fixed.put(quit, 50, 50);
        add(fixed);        
        
        setSizeRequest(250, 200);
    }
    
    public static void main(String[] args) {

        Gtk.init(args);
        new GButton();
        Gtk.main();
    }
}
</pre>

<p>
In our code example, we react to two events. Delete event and click event. 
The delete event is triggered, when we close the window. By default, the
window is destroyed but the application does not quit. We must explicitly
quit the program. 
</p>

<pre class="explanation">
 connect(new Window.DeleteEvent() {
     public boolean onDeleteEvent(Widget source, Event event) {
         Gtk.mainQuit();
         return false;
     }
 });
</pre>

<p>
If we click on the close button of the window, the window is destroyed.
But the application is not fully destroyed. We must call the
<b class="keyword">Gtk.mainQuit()</b> to end the application. 
We use the <b class="keyword">connect()</b> method to connect a 
callback method to a specific event type. In our case it is 
the <b class="keyword">DeleteEvent</b>.
</p>

<pre class="explanation">
  quit.connect(new Button.Clicked() {
      public void onClicked(Button button) {
          Gtk.mainQuit();
      }
  });
</pre>

<p>
When we click on the button widget, the <b class="keyword">onClicked()</b>
method is triggered. We quit the application in reaction to the button
click. 
</p>


<h2>Moving window</h2>

<p>
The next example shows, how we react to move events of a window. 
We show the current position of the upper left corner of the window 
in the titlebar. 
</p>

<div class="codehead">move.java</div> 
<pre class="code">
package com.zetcode;

import org.gnome.gdk.Event;
import org.gnome.gdk.EventConfigure;
import org.gnome.gtk.Gtk;
import org.gnome.gtk.Widget;
import org.gnome.gtk.Window;
import org.gnome.gtk.WindowPosition;

/**
 * ZetCode Java Gnome tutorial
 *
 * This program demonstrates the 
 * configure event.
 *
 * @author jan bodnar
 * website zetcode.com
 * last modified March 2009
 */

public class GMoveWindow extends Window 
        implements Window.ConfigureEvent {


    public GMoveWindow()  {

                
        connect(new Window.DeleteEvent() {
            public boolean onDeleteEvent(Widget source, Event event) {
                Gtk.mainQuit();
                return false;
            }
        });
 
        connect(this);
        
        setPosition(WindowPosition.CENTER);
        
        setTitle("");
        resize(250, 200);
        showAll();
    }
    
    public boolean onConfigureEvent(Widget widget,
        EventConfigure eventConfigure) {
                                    
        int x = eventConfigure.getX();
        int y = eventConfigure.getY();
        
        setTitle(x + ", " + y);
        return false;
    }
    
    public static void main(String[] args) {
        Gtk.init(args);
        new GMoveWindow();
        Gtk.main();
    }
}
</pre>

<p>
Resizing and moving windows end up in <b class="keyword">ConfigureEvent</b> being
created. 
</p>

<pre class="explanation">
 public boolean onConfigureEvent(Widget widget,
     EventConfigure eventConfigure) {
                                    
     int x = eventConfigure.getX();
     int y = eventConfigure.getY();
        
     setTitle(x + ", " + y);
     return false;
 }
</pre>

<p>
We set the x, y coordinates of the window to the titlebar
of the window. 
</p>


<br>
<img src="/img/gui/javagnome/move.png" alt="Move event">
<div class="figure">Figure: Move event</div>
<br>



<h2>EnterNotifyEvent</h2>

<p>
<b class="keyword">EnterNotifyEvent</b>
is emitted, when we enter the area of a widget with a mouse pointer. 
</p>

<div class="codehead">enter.java</div>
<pre class="code">
package com.zetcode;

import org.gnome.gdk.Color;
import org.gnome.gdk.Event;
import org.gnome.gdk.EventCrossing;
import org.gnome.gtk.Button;
import org.gnome.gtk.Fixed;
import org.gnome.gtk.Gtk;
import org.gnome.gtk.StateType;
import org.gnome.gtk.Widget;
import org.gnome.gtk.Window;
import org.gnome.gtk.WindowPosition;

/**
 * ZetCode Java Gnome tutorial
 *
 * This program demonstrates the 
 * EnterNotifyEvent.
 *
 * @author jan bodnar
 * website zetcode.com
 * last modified March 2009
 */

public class GEnterNotifyEvent extends Window {

    private Color lightGray = new Color(55000, 55000, 55000);


    public GEnterNotifyEvent() {
    
        setTitle("EnterNotifyEvent");
        
        initUI();
        
        connect(new Window.DeleteEvent() {
            public boolean onDeleteEvent(Widget source, Event event) {
                Gtk.mainQuit();
                return false;
            }
        });
    
        setDefaultSize(250, 150);
        setPosition(WindowPosition.CENTER);
        showAll();
    }
    
    
    
    public void initUI() {
    
        Button button = new Button("Button");
        button.setSizeRequest(80, 30);
        
        button.connect(new Button.EnterNotifyEvent() {
            public boolean onEnterNotifyEvent(Widget widget,
                        EventCrossing eventCrossing) {

                widget.modifyBackground(StateType.PRELIGHT, lightGray);
        
                return false;
            }
        });

        Fixed fix = new Fixed();
        fix.put(button, 20, 20);
        add(fix);
    }
    
    public static void main(String[] args) {
        Gtk.init(args);
        new GEnterNotifyEvent();
        Gtk.main();
    }
}
</pre>

<p>
We will change the background color of the button widget, once we hover a mouse pointer over it. 
</p>

<pre class="explanation">
  button.connect(new Button.EnterNotifyEvent() {
      public boolean onEnterNotifyEvent(Widget widget,
          EventCrossing eventCrossing) {
          widget.modifyBackground(StateType.PRELIGHT, lightGray);
          return false;
      }
  });
</pre>

<p>
Here we react to the <b class="keyword">EnterNotifyEvent</b>. 
</p>

<pre class="explanation">
 widget.modifyBackground(StateType.PRELIGHT, lightGray);
</pre>

<p>
We modify the color of the button's background. 
</p>

<hr class="btm">

<p>
This chapter was about events in Java Gnome.
</p>

<br>
<div class="center"> 
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* horizontal */
google_ad_slot = "1734478269";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script> 
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> 
</script> 
</div> 
<br>


<div class="botNav, center">
<span class="botNavItem"><a href="/">Home</a></span> ‡ <span class="botNavItem"><a href="..">Contents</a></span> ‡ 
<span class="botNavItem"><a href="#">Top of Page</a></span>
</div>


<div class="footer">
<div class="signature">
<a href="/">ZetCode</a> last modified March 12, 2009  <span class="copyright">&copy; 2007 - 2012 Jan Bodnar</span>
</div>
</div>

</div> <!-- content -->

</div> <!-- container -->

</body>
</html>

